package com.srwl.mytx.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextClock;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.R;
import com.srwl.mytx.utils.Utils;


/**
 * 协议对话框
 */
public class AgreementDialog extends AlertDialog {

    private Context context;
    private int arrayContent;
    private String title;

    public AgreementDialog(Context context) {
        super(context);
        this.context = context;
    }

    public AgreementDialog(Context context, int arrayContent) {
        super(context);
        this.context = context;
        this.arrayContent = arrayContent;
    }

    public AgreementDialog(Context context, String title, int arrayContent) {
        super(context);
        this.context = context;
        title = title;
        this.arrayContent = arrayContent;
    }


    protected AgreementDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    /*protected AgreementDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }*/

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_create_live_room_agreement);

        if (!TextUtils.isEmpty(title)) {
            TextView tv_title = findViewById(R.id.tv_title);
            tv_title.setText(title);
        }
        Button btnAgree = findViewById(R.id.btn_agree);
        btnAgree.setOnClickListener(v -> dismiss());
        TextView tvContent = findViewById(R.id.tv_content);
        CheckBox checkBox = findViewById(R.id.check_box);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btnAgree.setEnabled(isChecked);
                if (isChecked) {
                    btnAgree.setBackgroundResource(R.drawable.tx_btn_shape_white);
                } else {
                    btnAgree.setBackgroundResource(R.drawable.tx_btn_shape_gray);
                }
            }
        });
        String[] stringArray = context.getResources().getStringArray(arrayContent);
        StringBuilder agreement = new StringBuilder();
        agreement.append("    ");
        for (String s : stringArray) {
            agreement.append(s).append("\n    ");
        }
        tvContent.setText(agreement.toString());
    }


//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.layout_create_live_room_agreement, container);
//        Button btnAgree = view.findViewById(R.id.btn_agree);
//        btnAgree.setOnClickListener(v -> dismiss());
//        TextView tvContent = view.findViewById(R.id.tv_content);
//        CheckBox checkBox = view.findViewById(R.id.check_box);
//        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                btnAgree.setEnabled(isChecked);
//                if (isChecked) {
//                    btnAgree.setBackgroundResource(R.drawable.tx_btn_shape_white);
//                } else {
//                    btnAgree.setBackgroundResource(R.drawable.tx_btn_shape_gray);
//                }
//            }
//        });
//        String[] stringArray = getActivity().getResources().getStringArray(R.array.create_live_room_agreement);
//        StringBuilder agreement = new StringBuilder();
//        agreement.append("    ");
//        for (String s : stringArray) {
//            agreement.append(s).append("\n    ");
//        }
//        tvContent.setText(agreement.toString());
//        return view;
//    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        Window win = getDialog().getWindow(); // 一定要设置Background，如果不设置，window属性设置无效
//        // win.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        win.setLayout(dm.widthPixels, dm.heightPixels - Utils.dpToPx(60, getContext()));
//        // WindowManager.LayoutParams params = win.getAttributes();
//        // params.gravity = Gravity.CENTER; // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
////        params.width =dm.widthPixels-dm.densityDpi*10 ;
////        params.height = dm.heightPixels-dm.densityDpi*10 ;
////        win.setAttributes(params);
//    }
}
